/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.ta.entity.Prinfo;
import com.thinkgem.jeesite.modules.ta.service.PrinfoService;

/**
 * 优先级Controller
 * @author 刘雨轩
 * @version 2017-06-12
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/prinfo")
public class PrinfoController extends BaseController {

	@Autowired
	private PrinfoService prinfoService;
	
	@ModelAttribute
	public Prinfo get(@RequestParam(required=false) String id) {
		Prinfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = prinfoService.get(id);
		}
		if (entity == null){
			entity = new Prinfo();
		}
		return entity;
	}
	
	@RequiresPermissions("ta:prinfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(Prinfo prinfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Prinfo> page = prinfoService.findPage(new Page<Prinfo>(request, response), prinfo); 
		User user = UserUtils.getUser();
		List<String> ls = prinfoService.findQName(user.getNo());
		model.addAttribute("page", page);
		model.addAttribute("ls", ls);
		return "modules/ta/prinfoList";
	}

	@RequiresPermissions("ta:prinfo:view")
	@RequestMapping(value = "form")
	public String form(Prinfo prinfo, Model model) {
		model.addAttribute("prinfo", prinfo);
		return "modules/ta/prinfoForm";
	}

	@RequiresPermissions("ta:prinfo:edit")
	@RequestMapping(value = "save",produces="application/json;charset=utf-8")
	public @ResponseBody Object save(Prinfo prinfo) {
		prinfoService.upd(prinfo);
		return null;
	}
	
	@RequiresPermissions("ta:prinfo:edit")
	@RequestMapping(value = "delete")
	public String delete(Prinfo prinfo, RedirectAttributes redirectAttributes) {
		prinfoService.delete(prinfo);
		addMessage(redirectAttributes, "删除优先级成功");
		return "redirect:"+Global.getAdminPath()+"/ta/prinfo/?repage";
	}

}